package com.zhike.b2b.util;

import java.io.UnsupportedEncodingException;

import tools.util.Pinyin4J;

public class StringUtil
{ 
	/**
	 * 获取中文字符串的第一个拼音字母
	 * @param paramString
	 * @return
	 */
	  public static String getFirstStr(String paramString)
	  {
		 String py = "";
		 if(paramString.length()>0)
	        py = Pinyin4J.getOnePinyin(paramString.substring(0,1));
	    return py.substring(0,1);
	  }
	  
	public static String truncate(String oristr, int length)
	{
		return truncate(oristr,length,false);
	}
	
  public static String truncate(String oristr, int length, boolean paramBoolean)
  {
    length *= 2;
    int i = getEngLength(oristr);
    if (i <= length)
      return oristr;
    while (i > length)
    {
      oristr = oristr.substring(0, oristr.length() - 1);
      i = getEngLength(oristr);
    }
    if (paramBoolean)
      oristr = oristr + "...";
    return oristr;
  }

  public static int getEngLength(String paramString)
  {
    try
    {
      return new String(paramString.getBytes("GBK"), "iso-8859-1").length();
    }
    catch (UnsupportedEncodingException ex)
    {
      ex.printStackTrace();
    }
    return 0;
  }
}

